package com.kk.permission.huawei.actionutil;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kk.R;
import com.kk.permission.PermissionGuideUtil;
import com.kk.permission.accessibilityutils.NodeUtil;
import com.kk.permission.huawei.HuaweiActionUtil;
import com.kk.permission.huawei.HuaweiTool;

import java.util.HashMap;
import java.util.List;

public class HuaweiFloatWindowUtil {
    public static final int PAGE_CHANGE_TIME = 500;
    private static String TAG = "HuaweiFloatWindowUtil";
    private final String STEP1 = "step1";
    private final String STEP2 = "step2";
    private final String STEP3 = "step3";
    private final String STEP4 = "step4";
    private final String STEP5 = "step5";
    private final String STEP6 = "step6";
    private boolean isStep1 = false;
    private boolean isStep2 = false;
    private String mAppName = HuaweiTool.getAppName();
    private HashMap<String, String> mStepMap = new HashMap();
    private boolean result = false;

    public boolean floatWindow(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getString(R.string.accessiblity_permission_huawei_toast_window))) {
            if (this.mStepMap.containsKey("step1")) {
                this.result = true;
                HuaweiTool.actionFloatWindowDone();
                this.mStepMap.put("step2", "1");
                NodeUtil.back(accessibilityService);
            } else {
                List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(HuaweiTool.getAppName());
                if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                    if (!this.mStepMap.containsKey("step1")) {
                        HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                    }
                } else if (!this.mStepMap.containsKey("step1")) {
                    this.mStepMap.put("step1", "1");
                    NodeUtil.clickByText(accessibilityNodeInfo, HuaweiTool.getAppName());
                }
            }
        }
        return false;
    }

    public boolean floatWindowSDK26(AccessibilityNodeInfo accessibilityNodeInfo, AccessibilityService accessibilityService) {
        if (this.result) {
            return true;
        }
        if (this.mStepMap.containsKey("step3")) {
            if (!this.isStep1) {
                PermissionGuideUtil.sleep(500);
                NodeUtil.back(accessibilityService);
                this.isStep1 = true;
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, "应用管理")) {
            List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(HuaweiTool.getAppName());
            if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
                HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
            } else if (NodeUtil.pageContains(accessibilityNodeInfo, HuaweiTool.getAppName())) {
                NodeUtil.clickByText(accessibilityNodeInfo, HuaweiTool.getAppName());
                this.mStepMap.put("step1", "1");
            } else {
                HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
            }
        }
        if (this.mStepMap.containsKey("step3")) {
            if (!this.isStep2) {
                PermissionGuideUtil.sleep(500);
                NodeUtil.back(accessibilityService);
                this.isStep2 = true;
            }
        } else if (NodeUtil.pageContains(accessibilityNodeInfo, "应用信息")) {
            if (NodeUtil.pageContains(accessibilityNodeInfo, "显示在其他应用的上层") || NodeUtil.pageContains(accessibilityNodeInfo, "在其他应用的上层显示")) {
                if (NodeUtil.pageContains(accessibilityNodeInfo, "显示在其他应用的上层")) {
                    NodeUtil.clickByText(accessibilityNodeInfo, "显示在其他应用的上层");
                    HuaweiTool.actionFloatWindowDone();
                    this.mStepMap.put("step2", "1");
                } else {
                    HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                }
                if (NodeUtil.pageContains(accessibilityNodeInfo, "在其他应用的上层显示")) {
                    NodeUtil.clickByText(accessibilityNodeInfo, "在其他应用的上层显示");
                    HuaweiTool.actionFloatWindowDone();
                    this.mStepMap.put("step2", "1");
                } else {
                    HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
                }
            } else {
                HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
            }
        }
        if (NodeUtil.pageContains(accessibilityNodeInfo, "在其他应用上层显示")) {
            if (NodeUtil.pageContains(accessibilityNodeInfo, "在其他应用上层显示")) {
//                NodeUtil.performSwitch(accessibilityNodeInfo, "在其他应用上层显示");
                HuaweiActionUtil.performSwitch(accessibilityNodeInfo, "在其他应用上层显示");
                HuaweiTool.actionFloatWindowDone();
                this.mStepMap.put("step3", "1");
                NodeUtil.back(accessibilityService);
            } else {
                HuaweiActionUtil.scrollForward(accessibilityNodeInfo);
            }
        }
        return false;
    }
}
